import { createRouter, createWebHistory } from 'vue-router';
import LoginPage from '@/views/Login.vue';
import RegisterPage from '@/views/Register.vue';
import SampleList from '@/views/SampleList.vue';
import ProfilePage from '@/views/Profile.vue';
// 预留新增/编辑页面
// import SampleEdit from '@/views/SampleEdit.vue';

const routes = [
  { path: '/', redirect: '/sample' },
  { path: '/login', component: LoginPage },
  { path: '/register', component: RegisterPage },
  { path: '/sample', component: SampleList },
  { path: '/profile', component: ProfilePage, meta: { requiresAuth: true } },
  // { path: '/sample/add', component: SampleEdit, meta: { requiresAuth: true } },
  // { path: '/sample/edit/:id', component: SampleEdit, meta: { requiresAuth: true } },
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (to.meta.requiresAuth) {
    if (!token) {
      next({ path: '/login', query: { redirect: to.fullPath } });
    } else {
      next();
    }
  } else {
    next();
  }
});

export default router; 